home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / calc / alloc.h next >
C/C++ Source or Header  |  1995-07-17  |  3KB  |  122 lines

  1. /*
  2.  * Copyright (c) 1993 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Allocator definitions (fast malloc and free)
  7.  */
  8.  
  9. #if !defined(CALC_MALLOC)
  10.  
  11. #include "have_malloc.h"
  12. #ifdef HAVE_MALLOC_H
  13. # include <malloc.h>
  14. #else
  15. # if defined(__STDC__)
  16.    extern void *malloc();
  17.    extern void *realloc();
  18.    extern void free();
  19. # else
  20.    extern char *malloc();
  21.    extern char *realloc();
  22.    extern void free();
  23. # endif
  24. #endif
  25.  
  26. #include "have_string.h"
  27.  
  28. #ifdef HAVE_STRING_H
  29. # include <string.h>
  30.  
  31. #else
  32.  
  33. # ifdef OLD_BSD
  34. extern void bcopy();
  35. extern void bfill();
  36. extern char *index();
  37. # else /* OLD_BSD */
  38. extern void memcpy();
  39. extern void memset();
  40. #  if defined(__STDC__)
  41. extern void *strchr();
  42. #  else
  43. extern char *strchr();
  44. #  endif
  45. # endif /* OLD_BSD */
  46. extern void strcpy();
  47. extern void strncpy();
  48. extern void strcat();
  49. extern int strcmp();
  50. extern long strlen();    /* should be size_t, but old systems don't have it */
  51.  
  52. #endif
  53.  
  54. #ifdef OLD_BSD
  55. #undef memcpy
  56. #define memcpy(s1, s2, n) bcopy(s2, s1, n)
  57. #undef memset
  58. #define memset(s, c, n) bfill(s, n, c)
  59. #undef strchr
  60. #define strchr(s, c) index(s, c)
  61. #endif
  62.  
  63. #ifdef DONT_HAVE_VSPRINTF
  64. /*
  65.  * XXX - hack aleart
  66.  *
  67.  * Systems that do not have vsprintf() need something.  In some cases
  68.  * the sprintf function will deal correctly with the va_alist 3rd arg.
  69.  * Hope for the best!
  70.  */
  71. #define vsprintf sprintf
  72. #endif
  73.  
  74. #define mem_alloc malloc
  75. #define mem_realloc realloc
  76. #define mem_free free
  77.  
  78. #else /*!CALC_MALLOC*/
  79.  
  80. #define malloc(a) mem_alloc((long) a)
  81. #define realloc(a,b) mem_realloc((char *) a, (long) b)
  82. #define free(a) mem_free((char *) a)
  83. extern char *mem_alloc();
  84. extern char *mem_realloc();
  85. extern int mem_free();        /* MUST be int even though no return value */
  86.  
  87. #endif /*!CALC_MALLOC*/
  88.  
  89.  
  90. /*
  91.  * An item to be placed on a free list.
  92.  * These items are overlayed on top of the actual item being managed.
  93.  * Therefore, the managed items must be at least this size!
  94.  * Also, all items on a single free list must be the same size.
  95.  */
  96. struct free_item {
  97.     struct free_item *next;            /* next item on free list */
  98. };
  99. typedef struct free_item FREEITEM;
  100.  
  101.  
  102. /*
  103.  * The actual free list header.
  104.  */
  105. typedef struct {
  106.     long        itemsize;    /* size of an item being managed */
  107.     long        maxfree;    /* maximum number of free items */
  108.     long        curfree;    /* current number of free items */
  109.     FREEITEM    *freelist;    /* the free list */
  110. } FREELIST;
  111.  
  112. #if defined(__STDC__)
  113. typedef void ALLOCITEM;
  114. #else
  115. typedef char ALLOCITEM;
  116. #endif
  117. extern ALLOCITEM * allocitem( /* FREELIST * */ );
  118. extern void freeitem( /* FREELIST *, char * */ );
  119. extern void mem_stats();
  120.  
  121. /* END CODE */
  122.